<?php

namespace Tasker\Models;

class TaskContextModel extends BaseModel
{

	// Types
	public function getTypes(array $where=array())
	{
		return $this->connection->task_type();
	}

	public function getType(array $where)
	{
		return $this->connection->task_type[$where];
	}

	public function addType($values)
	{
		return $this->connection->task_type()->insert((array) $values);
	}

	public function editType($values)
	{
		$id = $values['id'];
		unset($values['id']);
		return $this->connection->task_type[$id]->update((array) $values);
	}

	public function deleteType($id)
	{
		return $this->connection->task_type[$id]->delete();
	}

	// Priorities
	public function getPriorities(array $where=array())
	{
		return $this->connection->task_priority()->where($where)->order('priority');
	}

	public function getPriority(array $where)
	{
		return $this->connection->task_priority[$where];
	}

	public function addPriority($values)
	{
		return $this->connection->task_priority()->insert((array) $values);
	}

	public function editPriority($values)
	{
		$id = $values['id'];
		unset($values['id']);
		return $this->connection->task_priority[$id]->update((array) $values);
	}

	public function deletePriority($id)
	{
		return $this->connection->task_priority[$id]->delete();
	}

	// Statuses
	public function getStatuses(array $where=array())
	{
		return $this->connection->task_status()->where($where)->order('priority');
	}

	public function getStatus(array $where)
	{
		return $this->connection->task_status[$where];
	}

	public function addStatus($values)
	{
		$n_values = array(
			'name' => $values['name'],
			'completed' => ($values['completed'] !== false ? 'yes' : 'no'),
			'priority' => $values['priority']+1,
		);
		$affected = $this->connection->task_status()->insert($n_values);
		if ($affected === FALSE)
		{
			return false;
		}else{
			$this->connection->task_status()->where('priority >= '.$n_values['priority'].' AND id <> '.$affected['id'])->update(array('priority' => new \NotORM_Literal('priority+1')));
			return true;
		}
	}

	public function editStatus($values)
	{
		$values = (array)$values;
		$values['completed'] = ($values['completed'] !== false ? 'yes' : 'no');
		$role = $this->connection->task_status[$values['id']];
		if ($role['priority'] == $values['priority'] + 1) // not changed
		{
			unset($values['priority']);
			$affected = $this->connection->task_status[$values['id']]->update($values);
		} else // changed
		{
			if($values['priority'] > $role['priority'])
				$values['priority']--;
			$this->connection->task_status()->where('priority > '.$role['priority'])->update(array('priority' => new \NotORM_Literal('priority-1')));
			$this->connection->task_status()->where('priority > '.$values['priority'])->update(array('priority' => new \NotORM_Literal('priority+1')));
			
			$values['priority']++;
			$affected = $this->connection->task_status[$values['id']]->update($values);
		}
		if ($affected === FALSE)
		{
			return false;
		}
		return true;
	}

	public function deleteStatus($id)
	{
		$role = $this->connection->task_status[$id];
		$affected = $this->connection->task_status[$id]->delete();
		if ($affected === FALSE)
		{
			return false;
		}else{
			$this->connection->task_status()->where('priority > '.$role['priority'])->update(array('priority' => new \NotORM_Literal('priority-1')));
			return true;
		}
	}

}
